#include "toptitle.h"
#include "ui_toptitle.h"

TopTitle::TopTitle(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TopTitle)
{
    ui->setupUi(this);
    ui->logo->installEventFilter(this);
    setTrainStat(0);
    setHighVoltage(0);
#ifndef RUN_IN_BOARD
    updateHighestLevelStat(1);
#endif
    setTrainSpeed(0);
#ifdef RUN_IN_BOARD
    updateHighestLevelStat(0);
    ui->CurStation->setText("");
    ui->NextStation->setText("");
    ui->DestStation->setText("");
    ui->MaintModeLabel->hide();
#endif
}

TopTitle::~TopTitle()
{
    delete ui;
}

bool TopTitle::eventFilter(QObject *obj, QEvent *event)
{
    if(obj == ui->logo && event->type() == QEvent::MouseButtonPress)
    {
        emit showOperationHelp(Page_Operation_Help);
    }
    return QWidget::eventFilter(obj, event);
}

void TopTitle::setTitleName(QString strName)
{
    ui->CurPageCN->setText(strName);
}

void TopTitle::setDriveMode(int mode)
{
    ui->DriveMode->setStyleSheet("color: rgb(0, 255, 0);\nfont:25pt 'arial';");
    QString mode_str = "?";
    switch (mode) {
    case 0:
        ui->DriveMode->setStyleSheet("color: rgb(255, 255, 255);\nfont:30pt 'arial';");
        mode_str = "?";
        break;
    case 1:
        mode_str = "WM";
        break;
    case 2:
        mode_str = "EUM";
        break;
    case 3:
        mode_str = "RM";
        break;
    case 4:
        mode_str = "CM";
        break;
    case 5:
        mode_str = "RRM";
        break;
    case 6:
        mode_str = "FAM";
        GlobalConfig::addOperationInfo(QString("列车进入%1模式").arg(mode_str));
        break;
    case 7:
        mode_str = "CAM";
        break;
    case 8:
        mode_str = "ATB";
        break;
    case 9:
        mode_str = "AM";
        break;
    case 10:
        ui->DriveMode->setStyleSheet("color: rgb(0, 255, 0);\nfont:20pt 'arial';");
        mode_str = "紧急牵引";
        break;
    default:
        ui->DriveMode->setStyleSheet("color: rgb(255, 255, 255);\nfont:30pt 'arial';");
        mode_str = "?";
        break;
    }
    ui->DriveMode->setText(mode_str);
}

void TopTitle::setTrainSpeed(uint16_t speed)
{
    if(speed == 0xFFFF)
    {
        ui->TrainSpeed->setStyleSheet("color: rgb(255,255,255);font:25pt 'arial';");
        ui->TrainSpeed->setText("?");
    }else
    {
        ui->TrainSpeed->setStyleSheet("color: rgb(0,255,0);font:25pt 'arial';");
        ui->TrainSpeed->setText(QString("%1").arg(speed/100));
        GlobalConfig::trainSpeed = speed/100;
    }
}

void TopTitle::setTrainStat(uint8_t stat)
{
    ui->TrainStat->setStyleSheet("color: rgb(0, 255, 0);font:20pt 'arial';");
    ui->EffDmd->setStyleSheet("color: rgb(0, 255, 0);font:25pt 'arial';");
    switch (stat) {
    case 0:
        ui->TrainStat->setStyleSheet("color: rgb(255,255,255);font:30pt 'arial';");
        ui->EffDmd->setStyleSheet("color: rgb(255, 255, 255);font:25pt 'arial';");
        ui->TrainStat->setText("?");
        break;
    case 1:
        ui->TrainStat->setStyleSheet("color: rgb(255,0,0);font:20pt 'arial';");
        ui->EffDmd->setStyleSheet("color: rgb(255,0,0);font:25pt 'arial';");
        ui->TrainStat->setText("紧急制动");
        break;
    case 2:
        ui->TrainStat->setStyleSheet("color: rgb(255,255,0);font:20pt 'arial';");
        ui->EffDmd->setStyleSheet("color: rgb(255, 255, 0);font:25pt 'arial';");
        ui->TrainStat->setText("快速制动");
        break;
    case 3:
        ui->TrainStat->setStyleSheet("color: rgb(255,255,0);font:20pt 'arial';");
        ui->EffDmd->setStyleSheet("color: rgb(255, 255, 0);font:25pt 'arial';");
        ui->TrainStat->setText("制动");
        break;
    case 4:
        ui->TrainStat->setText("牵引");
        break;
    case 5:
        ui->TrainStat->setText("惰行");
        break;
    case 6:
        ui->TrainStat->setText("停止");
        break;
    }
}

void TopTitle::setSpeedLimt(uint8_t speed)
{
    ui->TrainSpeedLimit->setText(QString("%1").arg(speed));
}

void TopTitle::setEffDmd(uint16_t value)
{
    ui->EffDmd->setText(QString("%1%").arg(value/100));
}

//void TopTitle::setTrainID(uint8_t id)
//{
//    m_nTrainID = id;
//    ui->TrainID->setText(QString("%1%2").arg(m_nLineID,2,10,QChar('0')).arg(id,2,10,QChar('0')));
//}

//void TopTitle::setLineID(uint8_t id)
//{
//    m_nLineID= id;
//    ui->TrainID->setText(QString("%1%2").arg(id,2,10,QChar('0')).arg(m_nTrainID,2,10,QChar('0')));
//}

void TopTitle::setDateTime()
{
    QDateTime time = QDateTime::currentDateTime();
    ui->DateTime->setText(time.toString("yyyy-MM-dd\nhh:mm:ss"));
}

void TopTitle::setHighVoltage(uint16_t value)
{
    if(value == 0xFFFF)
    {
        ui->HighVolt->setStyleSheet("color: rgb(255,255,255);font:25pt 'arial';");
        ui->HighVolt->setText("?");
    }else
    {
        ui->HighVolt->setStyleSheet("color: rgb(0,255,0);font:25pt 'arial';");
        ui->HighVolt->setText(QString("%1").arg(value));
    }

}

void TopTitle::updateHighestLevelStat(int level)
{
    ui->WarningLevel->show();
    if(level == 0)
    {
        ui->WarningLevel->hide();
        return;
    }
    else if(level == 1)
        ui->WarningLevel->setStyleSheet("border-image: url(:/Images/Fault_Lv1.png);");
    else if(level == 2)
        ui->WarningLevel->setStyleSheet("border-image: url(:/Images/Fault_Lv2.png);");
    else if(level == 3)
        ui->WarningLevel->setStyleSheet("border-image: url(:/Images/Fault_Lv3.png);");
}

void TopTitle::setCurStation(int id)
{
    ui->CurStation->setText(GlobalConfig::getStationNameByID(id));
}

void TopTitle::setEndStation(int id)
{
    ui->DestStation->setText(GlobalConfig::getStationNameByID(id));
}

void TopTitle::setNextStation(int id)
{
    ui->NextStation->setText(GlobalConfig::getStationNameByID(id));
}

void TopTitle::setDestinationID(uint32_t id)
{
    m_nDestinationID = id;
    updateDestinationID(m_nATCSysMode,m_nDestinationID);
}

void TopTitle::updateDestinationID(uint8_t mode,uint32_t id)
{
    if(mode == 0)
       ui->DestinationID->setText("");
    else
    {
        if(id == 0xFFFFFFFF)
        {
            ui->DestinationID->setText("");
            return;
        }
        QString dest_id_str = "";
        for(int i=0;i<4;i++)
        {
            uint8_t ch = (id >> (24-8*i)) & 0xFF;
            dest_id_str += QString(char(ch));
        }
         ui->DestinationID->setText(dest_id_str);
    }
}

void TopTitle::setTripID(uint16_t tid)
{
    m_nTripID = tid;
    updateTripID(m_nATCSysMode,m_nTripID,m_nServiceID);
}

void TopTitle::setServiceID(uint16_t sid)
{
    m_nServiceID = sid;
    updateTripID(m_nATCSysMode,m_nTripID,m_nServiceID);
}

void TopTitle::updateTripID(uint8_t mode,uint16_t tid,uint16_t sid)
{
    if(mode == 0)
        ui->DestinationID->setText("");
    else
    {
        uint real_sid = sid==65535?0:sid;
        ui->TripID->setText(QString("T%1%2").arg(real_sid,3,10,QChar('0')).arg(tid,3,10,QChar('0')));
    }
}

void TopTitle::setATCSystemMode(uint8_t mode)
{
    m_nATCSysMode = mode;
    updateTripID(m_nATCSysMode,m_nTripID,m_nServiceID);
    updateDestinationID(m_nATCSysMode,m_nDestinationID);
}

void TopTitle::setMaintMode(bool xMaint)
{
    ui->MaintModeLabel->setVisible(xMaint);
}
